
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
using LPV_POS;
public partial class frmDM_TIENGIO2 : Form
{
    public string ma = "";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT
    DataTable tbMainDS = new DataTable();
    DataTable tbMain = new DataTable();
    Boolean iloadform = false;


    #region"    frmDM_TIENGIO2"
    public frmDM_TIENGIO2()
    {
        InitializeComponent();
        cpublic ocpublic = new cpublic();
        chkAuto_Change.Checked = ocpublic.Registry_Get("HOTEL_AUTO_CHANGE1") == "True";
        chkAuto_Change2.Checked = ocpublic.Registry_Get("HOTEL_AUTO_CHANGE3") == "True";

        decimal dHOTEL_AUTO_CHANGE2 = 0;
        decimal.TryParse(ocpublic.Registry_Get("HOTEL_AUTO_CHANGE2"), out dHOTEL_AUTO_CHANGE2);
         nudAuto_Change.Value = dHOTEL_AUTO_CHANGE2;

    }
    #endregion

    #region"    frmDM_TIENGIO2_Load"
    public void frmDM_TIENGIO2_Load(object sender, EventArgs e)
    {
        btnluu.Enabled = false;
        btnxoa.Enabled = false;
        this.showcombobox();
        this.showdataDS();
        iloadform = true;

        this.grdDM_TIENGIO3_SelectionChanged(null, null);
        

    }
    #endregion

    #region"    các hàm load dữ liệu"
    #region"    showdata"
    void showdata()
    {

        thuvien oclsData = new thuvien();//lớp truy xuat du lieu 
        tbMain = oclsData.SP_SelectDataTable("sp_DM_TIENGIO2",
                           new string[] { "@flag", "@OBJID" },
                           new object[] { 4, ma });

    }
    #endregion
    #region"    showdataDS"
    void showdataDS()
    {
        string[] param = new string[2];
        object[] value = new object[2];
        int ipos = 0;
        param[ipos] = "@flag";
        value[ipos++] = 4;
        param[ipos] = "@DM_BAN_LOAI_MA";
        value[ipos++] = cboDM_BAN_LOAI_MA.SelectedValue;

        //grdDM_TIENGIO3.AutoGenerateColumns = false;

        thuvien oclsData = new thuvien();//lớp truy xuat du lieu 
        tbMainDS = oclsData.SP_SelectDataTable("sp_DM_TIENGIO2", param, value);
        grdDM_TIENGIO3.DataSource = tbMainDS;
        grdDM_TIENGIO3.Refresh();


    }
    #endregion
    #region"    showinfo"
    void showinfo()
    {

        this.showdata();
        if (tbMain.Rows.Count == 0)//Nếu table rỗng thì gán các Control mặc định
        {
            tTu_Gio.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            tDen_Gio.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            numGIO_THU.Value = 0;
            //numT1.Value = 0;
            numT2.Value = 0;
            //numT3.Value = 0;
            //numT4.Value = 0;
            //numT5.Value = 0;
            //numT6.Value = 0;
            //numT7.Value = 0;
            numLAM_TRON_GIO.Value = 0;
            return;
        }
        if (tbMain.Rows[0]["TU_GIO"] + "" != "")
            tTu_Gio.EditValue = tbMain.Rows[0]["TU_GIO"];
        if (tbMain.Rows[0]["DEN_GIO"] + "" != "")
            tDen_Gio.EditValue = tbMain.Rows[0]["DEN_GIO"];
        numGIO_THU.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["GIO_THU"]).Replace("0-", "-"));
        //numT1.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T1"]).Replace("0-", "-"));
        numT2.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T2"]).Replace("0-", "-"));
        //numT3.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T3"]).Replace("0-", "-"));
        //numT4.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T4"]).Replace("0-", "-"));
        //numT5.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T5"]).Replace("0-", "-"));
        //numT6.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T6"]).Replace("0-", "-"));
        //numT7.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T7"]).Replace("0-", "-"));
        numLAM_TRON_GIO.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["LAM_TRON_GIO"]).Replace("0-", "-"));
        chk_tinh_USD.Checked = (tbMain.Rows[0]["TINH_GIA_USD"] + "" == "True");
        //cboDM_BAN_LOAI_MA.SelectedValue = tbMain.Rows[0]["DM_BAN_LOAI_MA"];
    }
    #endregion
    #region"    showcombobox"
    void showcombobox()
    {

        thuvien oclsData = new thuvien();
        //DataTable tbMA_LOAIDICHVU = oclsData.SP_SelectDataTable("sp_DM_TIENGIO2", new string[] { "@flag" }, new object[] { 5 });
        //cboMA_LOAIDICHVU.DataSource = tbMA_LOAIDICHVU;
        //if (tbMA_LOAIDICHVU.Rows.Count > 0)
        //    cboMA_LOAIDICHVU.SelectedIndex = 0;

        DataTable tbDM_BAN_LOAI = oclsData.SP_SelectDataTable("sp_DM_TIENGIO3", new string[] { "@flag" }, new object[] { 6 });
        cboDM_BAN_LOAI_MA.DataSource = tbDM_BAN_LOAI;
        if (tbDM_BAN_LOAI.Rows.Count > 0)
            cboDM_BAN_LOAI_MA.SelectedIndex = 0;
    }
    #endregion
    #endregion

    #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"
    #region"    btnDSthem_Click"
    private void btnDSthem_Click(object sender, EventArgs e)
    {
        tbMain = tbMainDS.Copy();
        tbMain.Clear();
        ma = "-1";
        this.showinfo();
        this.setStatusButton(1);
    }

    #endregion
    #region"    btnDSthoat_Click"
    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        //if (MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
        {
            foreach (Form frm in this.MdiParent.MdiChildren)
                if (frm.Name == "frmDM_TIENGIO3")
                    frm.Dispose();
                else if (frm.Name == "frmDM_TIENGIO5")
                    frm.Dispose();

            this.Dispose();
        }

    }
    #endregion

    #endregion

    #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "
    #region"    tao_ma_tu_dong"
    void tao_ma_tu_dong()
    {
    }

    #endregion
    #region"    btnluu_Click"
    private void btnluu_Click(object sender, EventArgs e)
    {
        if (this.save() > 0)
        {
            MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.showdataDS();
        }
    }

    #endregion
    #region"    save"
    int save()
    {
        int _iReasult = -1;
        if (this.ValidateSave())
        {
            thuvien oclsData = new thuvien();//lớp truy xuat du lieu
            _iReasult = oclsData.SP_Update("sp_DM_TIENGIO2",
                 new string[] { "@flag", "@OBJID", "@MA_LOAIDICHVU", "@GIO_THU", //"@T1", 
                                "@TU_GIO","@DEN_GIO",
                                "@T2", //"@T3", "@T4", "@T5", "@T6", "@T7", 
                                "@LAM_TRON_GIO", "@DM_BAN_LOAI_MA", "@TINH_GIA_USD" },
                 new object[] { 1, ma, 2, numGIO_THU.Value, //numT1.Value, 
                                tTu_Gio.EditValue, tDen_Gio.EditValue,
                                numT2.Value, //numT3.Value, numT4.Value, numT5.Value, numT6.Value, numT7.Value, 
                                numLAM_TRON_GIO.Value,cboDM_BAN_LOAI_MA.SelectedValue,chk_tinh_USD.Checked });
        }
        return _iReasult;
    }

    #endregion
    #region"    ValidateSave"
    Boolean ValidateSave()
    {
        return true;
    }

    #endregion
    #region"    delete"
    int delete()
    {
        thuvien oclsData = new thuvien();//lớp truy xuat du lieu    
        int _iReasult = oclsData.SP_Update("sp_DM_TIENGIO2",
                                 new string[] { "@flag", "@OBJID" },
                                 new object[] { 3, ma, });
        return _iReasult;
    }

    #endregion
    #region"    btnxoa_Click"
    private void btnxoa_Click(object sender, EventArgs e)
    {// TÍNH TIỀN GIỜ "GIỜ THỨ I, II, III TRỞ ĐI..." này
        if (MessageBox.Show("Bạn muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
            if (this.delete() > 0)
            {
                MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ma = "-1";
                this.showdataDS();
                this.setStatusButton(4);
            }
        }
    }

    #endregion
    #region"    sự kiên chọn trên lưới -- tblDanhSach"
    #region"    showSearch"
    void showSearch(object sender, EventArgs e)
    {
        if (sender != null)
        {
            tbMain = (DataTable)sender;
            grdDM_TIENGIO3.DataSource = tbMain;
            grdDM_TIENGIO3.Refresh();
        }
    }
    #endregion

    #region"    grdDM_TIENGIO3_SelectionChanged"
    private void grdDM_TIENGIO3_SelectionChanged(object sender, EventArgs e)
    {

        if (iloadform )
        {
            if (grvDM_TIENGIO3.FocusedRowHandle >= 0)
            {
                iloadform = false;
                ma = "-1";
                string _iSTT = grvDM_TIENGIO3.GetDataRow(grvDM_TIENGIO3.FocusedRowHandle)["STT"] + "";
                DataRow[] _dr = tbMainDS.Select("STT='" + _iSTT + "'");
                if (_dr.Length > 0)
                {
                    ma = _dr[0]["OBJID"] + "";
                    this.setStatusButton(2);
                }
                this.showinfo();
                iloadform = true;
            }
        }
    }

    #endregion
    #endregion
    #region "    setStatusButton"
    void setStatusButton(int i1New2Edt3Sch4Del5Sav)
    {
        btnDSthem.Enabled = i1New2Edt3Sch4Del5Sav != 1;
        btnxoa.Enabled = (i1New2Edt3Sch4Del5Sav != 1 && i1New2Edt3Sch4Del5Sav != 4 && i1New2Edt3Sch4Del5Sav != 3);

        btnluu.Enabled = i1New2Edt3Sch4Del5Sav != 5;
    }
    #endregion
    #region "  txt_KeyDown "
    private void txt_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            this.cbo_SelectedIndexChanged(null, null);
    }
    #endregion
    #region "  cbo_SelectionChanged "
    private void cbo_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    #endregion
    #region"    grdDM_TIENGIO3_DataError"
    private void grdDM_TIENGIO3_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion
    #region"    grd_DataError"
    private void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion

    private void tabDM_TIENGIO2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (tabDM_TIENGIO2.SelectedTab == tabPage1)
        {//ngay
            this.Addform(new frmDM_TIENGIO3());
            //this.Hide();
        }
        else if (tabDM_TIENGIO2.SelectedTab == tabPage2)
        {//dem
            this.Addform(new frmDM_TIENGIO5());
            //this.Hide();
        }
    }
    #endregion

    #region "   Addform"
    private void Addform(Form f)
    {
        bool exist = false;

        Form[] frms = this.MdiParent.MdiChildren;
        foreach (Form frm in frms)
        {
            if (frm.Name == f.Name)
            {
                exist = true;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
                frm.Activate();
                break;
            }
        }

        if (!exist)
        {
            f.MdiParent = this.MdiParent;
            f.Show();
            f.WindowState = FormWindowState.Maximized;
        }
    }


    #endregion

    private void frmDM_TIENGIO2_Activated(object sender, EventArgs e)
    {
        tabDM_TIENGIO2.SelectedTab = tbpDanhSach;
    }

    private void cboDM_BAN_LOAI_MA_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (iloadform)
            this.showdataDS();
    }

    private void grvDM_TIENGIO3_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
    {
        this.grdDM_TIENGIO3_SelectionChanged(null, null);
    }

    private void grdDM_TIENGIO3_Click(object sender, EventArgs e)
    {
        this.grdDM_TIENGIO3_SelectionChanged(null, null);
    }

    private void chkAuto_Chang_CheckedChanged(object sender, EventArgs e)
    {
        nudAuto_Change.Enabled = chkAuto_Change.Checked;
        if (iloadform)
        {
            cpublic ocpublic = new cpublic();
            ocpublic.Registry_Set("HOTEL_AUTO_CHANGE1", chkAuto_Change.Checked);
            ocpublic.Registry_Set("HOTEL_AUTO_CHANGE2", nudAuto_Change.Value);
        }
    }

    private void nudAuto_Change_EditValueChanged(object sender, EventArgs e)
    {
        if (iloadform)
        {
            cpublic ocpublic = new cpublic();
            ocpublic.Registry_Set("HOTEL_AUTO_CHANGE1", chkAuto_Change.Checked);
            ocpublic.Registry_Set("HOTEL_AUTO_CHANGE2", nudAuto_Change.Value);
        }
    }

    private void chkAuto_Change2_CheckedChanged(object sender, EventArgs e)
    {
        if (iloadform)
        {
            cpublic ocpublic = new cpublic();
            ocpublic.Registry_Set("HOTEL_AUTO_CHANGE3", chkAuto_Change2.Checked);
        }
    }
}
